
package com.kugou.game.sdk.utils;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.core.FolderManager;

/**
 * @Title: ZoomBitmapUtil.java
 * @Package com.kugou.pay.android.headphoto
 * @Description: 图片的缩放类
 * @author lidexian
 * @date 2013-7-8 下午2:46:52
 * @version V1.0
 */
public class ZoomBitmapUtil {

    public static final String PHOTO_PATH = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/KugouPay/picture";

    public static final int NONE = 0;

    public static final int PHOTOHRAPH = 1;// 拍照

    public static final int PHOTOZOOM = 2; // 缩放

    public static final int PHOTORESOULT = 3;// 结果

    public static final String IMAGE_UNSPECIFIED = "image/*";

    public static String SaveBitmap(User user, Bitmap bmp) {
        // File file = new File(PHOTO_PATH);
        String path = null;
        // if (!file.exists())
        // file.mkdirs();
        try {
            // path = file.getPath() + "/" + user.getNickName() + ".png";
            path = FolderManager.USER_HEAD_ICON;
            File file1 = new File(path);
            if (!file1.exists()) {
                file1.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(path, false);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    public static void startPhotoZoom(Context mContext, Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        // Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.parse("file://" + FolderManager.USER_HEAD_ICON));
        ((Activity) mContext).startActivityForResult(intent, PHOTORESOULT);
    }

    /*
     * 获取头像
     */
    public static Bitmap getHeadPhoto(User user) {
        Bitmap roundBitmap = null;
        // String imagePath = PHOTO_PATH + "/" + user.getNickName() + ".png";
        String imagePath = FolderManager.USER_HEAD_ICON;
        if (FileUtil.isFileExist(imagePath)) {
            Bitmap decodeFile = BitmapFactory.decodeFile(imagePath);
            roundBitmap = BitmapCornersUtil.toRoundBitmap(decodeFile);
        }
        return roundBitmap;
    }
}
